package com.mall.controller;

import com.mall.entity.Result;
import com.mall.entity.StatusCode;
import com.mall.pojo.FastDFSFile;
import com.mall.utils.FastDFSClient;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author: bamboo_shoot on 20/5/28
 * @description: _文件上传
 */
@RestController
@CrossOrigin
@RequestMapping("file")
public class FileController {
    private Logger logger = LoggerFactory.getLogger(FileController.class);

    @PostMapping("uploadFile")
    public Result uploadFile(MultipartFile file) {
        try {
            FastDFSFile fastDFSFile = new FastDFSFile(file.getOriginalFilename(), file.getBytes(), FilenameUtils.getExtension(file.getOriginalFilename()));
//        这里返回图片的一个地址名称
            String path = FastDFSClient.uploadFile(fastDFSFile);
            return new Result(true, StatusCode.OK, "图片上传成功", FastDFSClient.getTrackerUrl() + path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
